﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;

using AKMII.DMRA.Common;
using AKMII.DMRA.DataAccess;

namespace AKMII.DMRA.Business.Management
{
    public class LeadManager
    {
        private LeadAccessor leadAccessor;

        public LeadManager()
        {
            leadAccessor = new LeadAccessor();
        }

        public List<Pair<string, string>> GetCity()
        {
            List<Pair<string, string>> cities = new List<Pair<string, string>>();

            try
            {
                cities = leadAccessor.GetCity();
            }
            catch (Exception ex)
            {
                Logger.Error("Exception occurs in get city", ex);
            }

            return cities;
        }

        public List<string> GetCity(string mdnumber, string county)
        {
            List<string> cities = new List<string>();

            try
            {
                cities = leadAccessor.GetCity(mdnumber, county);
            }
            catch (Exception ex)
            {
                Logger.Error("Exception occurs in get city by mdnumber and terr", ex);
            }

            return cities;
        }

        public List<Pair<string, string>> GetCounty()
        {
            List<Pair<string, string>> counties = new List<Pair<string, string>>();

            try
            {
                counties = leadAccessor.GetCounty();
            }
            catch (Exception ex)
            {
                Logger.Error("Exception occurs in get county", ex);
            }

            return counties;
        }

        /*
        public List<Pair<string, string>> GetTerritory()
        {
            List<Pair<string, string>> territories = new List<Pair<string, string>>();

            try
            {
                territories = leadAccessor.GetTerritory(null, 0, 0);
            }
            catch (Exception ex)
            {
                Logger.Error("Exception occurs in get territory", ex);
            }

            return territories;
        }
        public List<Pair<string, string>> GetTerritory(string mdNumber, int month, int year)
        {
            List<Pair<string, string>> territories = new List<Pair<string, string>>();

            try
            {
                territories = leadAccessor.GetTerritory(mdNumber, month, year);
            }
            catch (Exception ex)
            {
                Logger.Error("Exception occurs in get territory for assignment", ex);
            }

            return territories;
        }
        
        */
        public List<Pair<string, string>> GetState()
        {
            List<Pair<string, string>> states = new List<Pair<string, string>>();

            try
            {
                states = leadAccessor.GetState();
            }
            catch (Exception ex)
            {
                Logger.Error("Exception occurs in get states", ex);
            }

            return states;
        }

        public List<Lead> GetLeads(string accountID, int month, int year)
        {
            List<Lead> leads = new List<Lead>();
            List<Lead> prosLeads = new List<Lead>();

            try
            {
                leads = leadAccessor.GetLeads(accountID, month, year, 0);

                //if (!string.IsNullOrEmpty(accountID))
                //{
                //    foreach (var lead in leads)
                //    {
                        //prosLeads.AddRange(GetProsLeads(lead));
                        //lead.RawData = string.Empty;
                //    }
                //}

                //leads.AddRange(prosLeads);
            }
            catch (Exception ex)
            {
                Logger.Error("Exception occurs in get leads", ex);
            }

            return leads;
        }

        public List<Lead> GetLeadsByLeadBlockCode(int id)
        {
            List<Lead> leads = new List<Lead>();
            List<Lead> prosLeads = new List<Lead>();

            try
            {
                leads = leadAccessor.GetLeads(id);

                foreach (var lead in leads)
                {
                    //prosLeads.AddRange(GetProsLeads(lead));
                    lead.RawData = string.Empty;
                }

                leads.AddRange(prosLeads);
            }
            catch (Exception ex)
            {
                Logger.Error("Exception occurs in get leads", ex);
            }


            return leads.Where(c => c.Longitude.Trim() != "0.0000" || c.Latitude != "0.0000").ToList<Lead>();
        }

        public List<Lead> GetLeadsByZipId(List<int> zipIds)
        {
            List<Lead> leads = new List<Lead>();

            try
            {
                if (zipIds != null && zipIds.Count > 0)
                {
                    string idString = string.Empty;

                    //for (int i = 0; i < zipIds.Count; i++)
                    //{
                    //    if (i == zipIds.Count - 1)
                    //    {
                    //        idString += zipIds[i];
                    //    }
                    //    else
                    //    {
                    //        idString += zipIds[i] + ",";
                    //    }
                    //}
                    idString = zipIds.Select(c => c.ToString()).ToList<string>().Aggregate((a, b) => { return a + "," + b; });
                    leads = leadAccessor.GetLeads(idString);

                    foreach (var lead in leads)
                    {
                        lead.RawData = string.Empty;
                    }

                }
            }
            catch (Exception ex)
            {
                Logger.Error("Exception occurs in get leads by lead block ids", ex);
            }

            return leads.Where(c => c.Longitude.Trim() != "0.0000" || c.Latitude != "0.0000").ToList<Lead>();
        }

        public List<Lead> GetAssignedLeads(string accountID, int month, int year)
        {
            List<Lead> leads = new List<Lead>();
            List<Lead> prosLeads = new List<Lead>();

            try
            {
                leads = leadAccessor.GetLeads(accountID, month, year, 1);

                if (!string.IsNullOrEmpty(accountID))
                {
                    foreach (var lead in leads)
                    {
                        //prosLeads.AddRange(GetProsLeads(lead));
                        lead.RawData = string.Empty;
                    }
                }

                leads.AddRange(prosLeads);
            }
            catch (Exception ex)
            {
                Logger.Error("Exception occurs in get leads", ex);
            }

            return leads;
        }

        /*
        private List<Lead> GetProsLeads(Lead lead)
        {
            List<Lead> prosLeads = new List<Lead>();

            for (int i = 0; i < 4; i++)
            {
                if (!(lead.LeadType == "B" && i == 0))
                {
                    if (!string.IsNullOrEmpty(lead.RawData.Substring(3109 + i * 197, 10).Trim()))
                    {
                        Lead prosLead = SetProsLead(lead.SubSequenceNumber, lead.SelPersonId, lead.LeadType, lead.PcLeadType, lead.RawData.Substring(3020 + i * 197, 30).Trim()
                            , lead.RawData.Substring(2990 + i * 197, 30).Trim().Split(new char[] { ' ' },
                            StringSplitOptions.None)[0], lead.RawData.Substring(2990 + i * 197, 30).Trim().Split(new char[] { ' ' },
                            StringSplitOptions.None)[lead.RawData.Substring(2990 + i * 197, 30).Trim().Split(new char[] { ' ' },
                            StringSplitOptions.None).Length - 1], lead.RawData.Substring(3050 + i * 197, 30).Trim(), lead.RawData.Substring(3080 + i * 197, 27).Trim(),
                            lead.RawData.Substring(3107 + i * 197, 2).Trim(), lead.RawData.Substring(3109 + i * 197, 10).Trim(), lead.RawData.Substring(3129 + i * 197, 1).Trim(),
                            lead.RawData.Substring(3119 + i * 197, 10).Trim(),
                            lead.LeadBlockCode, lead.Division, lead.Market, lead.Territory, lead.CallType, lead.MdNumber, lead.SequenceNumber,
                            lead.RawData.Substring(3131 + i * 197, 7).Trim(), lead.RawData.Substring(3169 + i * 197, 9).Trim(), lead.RawData.Substring(3178 + i * 197, 9).Trim(),
                            lead.BusPhone, lead.ResPhone, lead.RawData.Substring(3168 + i * 197, 1).Trim());
                        prosLeads.Add(prosLead);
                    }
                }
                else 
                {
                    lead.HomeBaseInd = lead.RawData.Substring(3168 + i * 197, 1);
                }
            }

            return prosLeads;
        }

        private Lead SetProsLead(string subSequenceNumber, string personId, string leadType, string pcLeadType, string firmName,
            string firstName, string lastName, string streetAddress, string city, string state, string zip, string dnc, string phone,
            string leadBlockCode, string division, string market, string territory, string callType, string mdNumber,
            string sequenceNumber, string birthDate, string latitude, string longitude, string busPhone, string resPhone, string homeBase)
        {
            Lead prosLead = new Lead();

            prosLead.SubSequenceNumber = subSequenceNumber;
            prosLead.SelPersonId = personId;
            prosLead.LeadType = leadType;
            if (leadType != "B")
            {
                prosLead.LeadType = "N";
            }
            prosLead.PcLeadType = pcLeadType;
            prosLead.FirmName = firmName;
            prosLead.FirstName = firstName;
            prosLead.LastName = lastName;
            prosLead.StreetAddress = streetAddress;
            prosLead.LeadCity = city;
            prosLead.LeadState = state;
            prosLead.Zip = zip;
            prosLead.SelDNC = dnc;
            prosLead.Telephone = phone;
            prosLead.LeadBlockCode = leadBlockCode;
            prosLead.Division = division;
            prosLead.Market = market;
            prosLead.Territory = territory;
            prosLead.CallType = callType;
            prosLead.MdNumber = mdNumber;
            prosLead.SequenceNumber = sequenceNumber;
            prosLead.BirthDate = birthDate;
            prosLead.Latitude = latitude.Trim();
            prosLead.Longitude = longitude.Trim();
            prosLead.BusPhone = busPhone;
            prosLead.ResPhone = resPhone;
            prosLead.HomeBaseInd = homeBase;

            return prosLead;
        }
         * */
    }
}
